﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace WebApp
{
    public partial class Login_ : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112138;uid=2014112138;pwd=19960516";
            SqlConnection conn = new SqlConnection(strCn);
            conn.Open();
            
            if (Session["CheckCode"] != null)
            {
                string checkcode = Session["CheckCode"].ToString();
                if (this.TextBox3.Text == checkcode)
                {                    
                    if (rdStu.Checked)
                    {                       
                        string strSql = "select * from UserInfo where UserName='" + this.txtUserName.Text + "' and  Password='" + txtPwd.Text + "'";
                        SqlCommand cmd = new SqlCommand(strSql, conn);
                        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        string ID = Request.QueryString["Type"];

                        if (dr.Read())
                        {
                            Session["Tno"] = this.txtUserName.Text;
                            Session["Lock"] = true;

                            Response.Redirect("default.aspx");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('登录失败！用户名或密码错误！')", true);
                        }
                    }
                    else
                    {
                        if (rdMan.Checked)
                        {
                            Session["Tno"] = this.txtUserName.Text;
                            Session["Lock"] = true;

                            Response.Redirect("Manager.aspx");
                        }
                        else
                        {
                            string strSql = "select * from UserTeacher where TeaName='" + this.txtUserName.Text + "' and  Password='" + txtPwd.Text + "'";
                            SqlCommand cmd = new SqlCommand(strSql, conn);
                            SqlDataReader dd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                            string ID = Request.QueryString["Type"];

                            if (dd.Read())
                            {
                                Session["Tno"] = this.txtUserName.Text;
                                Session["Lock"] = true;

                                Response.Redirect("Teacher.aspx");
                            }
                            else
                            {
                                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('登录失败！用户名或密码错误！')", true);
                            }
                        }
                    }
                    conn.Close();
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('验证码输入错误!')", true);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            this.txtUserName.Text = "";
            this.txtPwd.Text = "";
            this.TextBox3.Text = "";
        }

        protected void btnIn_Click(object sender, EventArgs e)
        {
            if (rdStu.Checked)
            {
                Response.Redirect("StuIN.aspx");
            }
            else
            {
                if (rdMan.Checked)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('对不起！你没有注册管理员的权限！')", true);
                }
                else
                {
                    Response.Redirect("TeaIN.aspx");
                }
            }                      
        }

        
    }
}